//
//  CTTextManager.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTTextManager.h"
#import <CoreText/CoreText.h>
#import "CTAppLanguage.h"


static CTTextManager * textManager = nil;

@implementation CTTextManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

+(CTTextManager *)shareInstance
{
    @synchronized (self)
    {
        if (textManager == nil)
        {
            textManager = [[CTTextManager alloc] init];
        }
    }
    return textManager;
}



#pragma mark - 字体 -
-(UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size
{
    NSURL *fontUrl = [NSURL fileURLWithPath:path];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(fontRef, NULL);
    NSString * fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;
}



#pragma mark - 字符 -
//判断字符串是否为空
- (BOOL)isBlankString:(NSString *)string
{
    @try {
        if (string == nil || string == NULL) {
            return YES;
        }
        if ([string isKindOfClass:[NSNull class]]) {
            return YES;
        }
        if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
            return YES;
        }
        if (string.length == 0) {
            return YES;
        }
    }
    @catch (NSException *exception) {
        return YES;
    }
    
    return NO;
}

//判断字符串是否含特定字符
-(BOOL)isFoundString:(NSString *)foundStr withStr:(NSString *)targetStr
{
    if ([targetStr rangeOfString:foundStr].location != NSNotFound) {
        return YES;
    }
    return NO;
}


-(BOOL)isMatch:(NSString *)str
{
    NSString * regex = @"^[A-Za-z0-9]{1,100}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    
    return isMatch;
}

-(int)charNumber:(NSString*)strtemp {
    
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
    }
    return strlength;
}

/*
 * 时间戳转换时间
 */

-(NSString *)timestampToTime:(NSString *)timestampStr
{
    if ([self isBlankString:timestampStr] == YES)return nil;
    long time = (long)[timestampStr longLongValue];
    
    NSString *str;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];
    
    //现在时间,你可以输出来看下是什么格式
    
    
    
    long nowTime = (long)[datenow timeIntervalSince1970];
    
    if ((nowTime - time)/600 < 1)
        
    {
        
        str  = [NSString stringWithFormat:@"刚刚"];
        
        return str;
        
    }
    
    else if((nowTime - time)/60 >= 10 && (nowTime - time)/60 <= 59)
        
    {
        
        str  = [NSString stringWithFormat:@"%d分钟前",(int)(nowTime - time)/60];
        
        return str;
        
    }
    
    else if ((nowTime - time)/3600 >= 1 && (nowTime - time)/3600 <= 23 )
        
    {
        
        str = [NSString stringWithFormat:@"%d小时前",(int)(nowTime - time)/3600];
        
        return str;
        
    }
    
//    else if ((nowTime - time)/86400 >= 1 && (nowTime - time)/86400 <= 2)
//        
//    {
//        
//        str = [NSString stringWithFormat:@"昨天"];
//        
//    }
    
    else
        
    {
        
        NSDate *nowData = [NSDate dateWithTimeIntervalSince1970:nowTime];
        
        NSDate *birData = [NSDate dateWithTimeIntervalSince1970:time];
        
        NSCalendar*calendar = [NSCalendar currentCalendar];
        
        NSDateComponents*comps;
        
        comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)fromDate:nowData];
        
        NSInteger nowYear = [comps year];
        
        comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)fromDate:birData];
        
        NSInteger biryear = [comps year];
        
        NSInteger birmonth = [comps month];
        
        NSInteger birday = [comps day];
        
        if (nowYear==biryear)
            
        {
            
            str = [NSString stringWithFormat:@"%ld月%ld日",(long)birmonth,(long)birday];
            
        }
        
        else
            
        {
            
            str = [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)biryear,(long)birmonth,(long)birday];
            
        }
        
        
        
        return str;
        
    }
    
    return nil;
    

}




@end